package com.irdstudio.efp.console.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.console.service.dao.GrantAuthInfoDao;
import com.irdstudio.efp.console.service.domain.GrantAuthDetail;
import com.irdstudio.efp.console.service.domain.GrantAuthInfo;
import com.irdstudio.efp.console.service.facade.GrantAuthInfoService;
import com.irdstudio.efp.console.service.vo.GrantAuthDetailVO;
import com.irdstudio.efp.console.service.vo.GrantAuthInfoVO;
/**
 * <p>ServiceImpl: 授权管理配置表				<p>
 * @author fuzm
 * @date 2018-05-07
 */
@Service("grantAuthInfoService")
public class GrantAuthInfoServiceImpl implements GrantAuthInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(GrantAuthInfoServiceImpl.class);

	@Autowired
	private GrantAuthInfoDao grantAuthInfoDao;
	
	/**
	 * 新增操作
	 */
	public GrantAuthInfoVO insertGrantAuth(GrantAuthInfoVO inGrantAuthInfoVo) {
		logger.debug("当前新增数据为:"+ inGrantAuthInfoVo.toString());
		GrantAuthInfoVO grantAuthInfoVo= null;
		int num = 0;
		try {
			GrantAuthInfo grantAuthInfo = new GrantAuthInfo();
			beanCopy(inGrantAuthInfoVo, grantAuthInfo);
			String grantNo = UUID.randomUUID().toString().replace("-", "");
			grantAuthInfo.setGrantNo(grantNo);
			num = grantAuthInfoDao.insertGrantAuthInfo(grantAuthInfo);
			if(num > 0){
				inGrantAuthInfoVo.setGrantNo(grantNo);
				grantAuthInfoVo =  queryByPk(inGrantAuthInfoVo);
			}
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return grantAuthInfoVo;
	}
	
	/**
	 * 新增操作
	 * @param inGrantAuthInfoVo
	 * @return
	 */
	public int insertGrantAuthInfo(GrantAuthInfoVO inGrantAuthInfoVo) {
		logger.debug("当前新增数据为:"+ inGrantAuthInfoVo.toString());
		int num = 0;
		try {
			GrantAuthInfo grantAuthInfo = new GrantAuthInfo();
			beanCopy(inGrantAuthInfoVo, grantAuthInfo);
			num = grantAuthInfoDao.insertGrantAuthInfo(grantAuthInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(GrantAuthInfoVO inGrantAuthInfoVo) {
		logger.debug("当前删除数据条件为:"+ inGrantAuthInfoVo);
		int num = 0;
		try {
			GrantAuthInfo grantAuthInfo = new GrantAuthInfo();
			beanCopy(inGrantAuthInfoVo, grantAuthInfo);
			GrantAuthDetail inGrantAuthDetail = new GrantAuthDetail();
			inGrantAuthDetail.setGrantNo(grantAuthInfo.getGrantNo());
			
			num = grantAuthInfoDao.deleteByPk(grantAuthInfo);
			if ( num > 0 )
				num += grantAuthInfoDao.deleteByGrantNo(inGrantAuthDetail);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inGrantAuthInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(GrantAuthInfoVO inGrantAuthInfoVo) {
		logger.debug("当前修改数据为:"+ inGrantAuthInfoVo.toString());
		int num = 0;
		try {
			GrantAuthInfo grantAuthInfo = new GrantAuthInfo();
			beanCopy(inGrantAuthInfoVo, grantAuthInfo);
			num = grantAuthInfoDao.updateByPk(grantAuthInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inGrantAuthInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 状态生效更新操作
	 */
	public int updateByCommit(GrantAuthInfoVO inGrantAuthInfoVo) {
		logger.debug("当前修改数据为:"+ inGrantAuthInfoVo.toString());
		int num = 0;
		try {
			GrantAuthInfo grantAuthInfo = new GrantAuthInfo();
			beanCopy(inGrantAuthInfoVo, grantAuthInfo);
			num = grantAuthInfoDao.updateByCommit(grantAuthInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inGrantAuthInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 状态失效更新操作
	 */
	public int updateByStatus(GrantAuthInfoVO inGrantAuthInfoVo) {
		logger.debug("当前修改数据为:"+ inGrantAuthInfoVo.toString());
		int num = 0;
		try {
			GrantAuthInfo grantAuthInfo = new GrantAuthInfo();
			beanCopy(inGrantAuthInfoVo, grantAuthInfo);
			num = grantAuthInfoDao.updateByStatus(grantAuthInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inGrantAuthInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	
	
	/**
	 * 查询操作
	 */
	public GrantAuthInfoVO queryByPk(GrantAuthInfoVO inGrantAuthInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inGrantAuthInfoVo);
		try {
			GrantAuthInfo queryGrantAuthInfo = new GrantAuthInfo();
			beanCopy(inGrantAuthInfoVo, queryGrantAuthInfo);
			
			GrantAuthInfo queryRslGrantAuthInfo = grantAuthInfoDao.queryByPk(queryGrantAuthInfo);
			if (Objects.nonNull(queryRslGrantAuthInfo)) {
				GrantAuthInfoVO outGrantAuthInfoVo = beanCopy(queryRslGrantAuthInfo, new GrantAuthInfoVO());
				logger.debug("当前查询结果为:"+ outGrantAuthInfoVo.toString());
				return outGrantAuthInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<GrantAuthInfoVO> queryAllOwner(GrantAuthInfoVO grantAuthInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<GrantAuthInfoVO> list = null;
		try {
			List<GrantAuthInfo> grantAuthInfos = grantAuthInfoDao.queryAllOwnerByPage(grantAuthInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ grantAuthInfos.size());
			pageSet(grantAuthInfos, grantAuthInfoVo);
			list = (List<GrantAuthInfoVO>) beansCopy(grantAuthInfos, GrantAuthInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<GrantAuthInfoVO> queryAllCurrOrg(GrantAuthInfoVO grantAuthInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<GrantAuthInfo> grantAuthInfos = grantAuthInfoDao.queryAllCurrOrgByPage(grantAuthInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+grantAuthInfos.size());
		List<GrantAuthInfoVO> list = null;
		try {
			pageSet(grantAuthInfos, grantAuthInfoVo);
			list = (List<GrantAuthInfoVO>) beansCopy(grantAuthInfos, GrantAuthInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<GrantAuthInfoVO> queryAllCurrDownOrg(GrantAuthInfoVO grantAuthInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<GrantAuthInfo> grantAuthInfos = grantAuthInfoDao.queryAllCurrDownOrgByPage(grantAuthInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ grantAuthInfos.size());
		List<GrantAuthInfoVO> list = null;
		try {
			pageSet(grantAuthInfos, grantAuthInfoVo);
			list = (List<GrantAuthInfoVO>) beansCopy(grantAuthInfos, GrantAuthInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	//授信明细接口实现
	/**
	 * 新增操作
	 */
	public int insertGrantAuthDetail(GrantAuthDetailVO inGrantAuthDetailVo) {
		logger.debug("当前新增数据为:"+ inGrantAuthDetailVo.toString());
		int num = 0;
		try {
			GrantAuthDetail grantAuthDetail = new GrantAuthDetail();
			beanCopy(inGrantAuthDetailVo, grantAuthDetail);
			String detailId = UUID.randomUUID().toString().replace("-", "");
			grantAuthDetail.setDetailId(detailId);
			
			int crdMinAmt = ( grantAuthDetail.getCrdMinAmt() ).intValue();
			int crdMaxAmt = ( grantAuthDetail.getCrdMaxAmt() ).intValue();
			
			num = queryByGrantVal(inGrantAuthDetailVo);
			if(num<1){
				List<GrantAuthDetail> grantAuthDetails = grantAuthInfoDao.queryByGrantNo(inGrantAuthDetailVo);
				if(grantAuthDetails.size() > 0){
					for(int i=0;i<grantAuthDetails.size();i++){
						int oldCrdMax = ( grantAuthDetails.get(i).getCrdMaxAmt() ).intValue();
						int oldCrdMix = ( grantAuthDetails.get(i).getCrdMinAmt() ).intValue();
						
						if(crdMinAmt <= oldCrdMix && crdMaxAmt > oldCrdMix){
							//最小授信金额和最大金额不能交叉
							return -2;
						}else if(crdMinAmt > oldCrdMix && crdMinAmt < oldCrdMax){
							//最小授信金额与最大金额不能交叉
							return -2;
						}
					}
					//通过校验则增加
					num = grantAuthInfoDao.insertGrantAuthDetail(grantAuthDetail);
					
				}else{
					num = grantAuthInfoDao.insertGrantAuthDetail(grantAuthDetail);
				}
			}else{
				num = -3;
			}
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByDetailId(GrantAuthDetailVO inGrantAuthDetailVo) {
		logger.debug("当前删除数据条件为:"+ inGrantAuthDetailVo);
		int num = 0;
		try {
			GrantAuthDetail grantAuthDetail = new GrantAuthDetail();
			beanCopy(inGrantAuthDetailVo, grantAuthDetail);
			num = grantAuthInfoDao.deleteByDetailId(grantAuthDetail);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inGrantAuthDetailVo +"删除的数据条数为"+ num);
		return num;
	}
	
	
	/**
	 * 根据授权Id删除操作
	 */
	public int deleteByGrantNo(GrantAuthDetailVO inGrantAuthDetailVo) {
		logger.debug("当前删除数据条件为:"+ inGrantAuthDetailVo);
		int num = 0;
		try {
			GrantAuthDetail grantAuthDetail = new GrantAuthDetail();
			beanCopy(inGrantAuthDetailVo, grantAuthDetail);
			num = grantAuthInfoDao.deleteByGrantNo(grantAuthDetail);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inGrantAuthDetailVo +"删除的数据条数为"+ num);
		return num;
	}



	/**
	 * 更新操作
	 */
	public int updateByDetailId(GrantAuthDetailVO inGrantAuthDetailVo) {
		logger.debug("当前修改数据为:"+ inGrantAuthDetailVo.toString());
		int num = 0;
		try {
			GrantAuthDetail grantAuthDetail = new GrantAuthDetail();
			beanCopy(inGrantAuthDetailVo, grantAuthDetail);
			int crdMinAmt = ( grantAuthDetail.getCrdMinAmt() ).intValue();
			int crdMaxAmt = ( grantAuthDetail.getCrdMaxAmt() ).intValue();
			//根据
			List<GrantAuthDetailVO> listGrantDetails = queryByGrantValAndGrantNo(inGrantAuthDetailVo);
			List<GrantAuthDetailVO> listDetails = new ArrayList<>();
			for (GrantAuthDetailVO grantAuthDetailVO : listGrantDetails) {
				if(!grantAuthDetailVO.getDetailId().equals(inGrantAuthDetailVo.getDetailId())){
					listDetails.add(grantAuthDetailVO);
				}
			}
			if(listDetails != null && listDetails.size()>0){
				//岗位或者用户授权已经存在，不能有相同的岗位或用户授权配置
				num = -3;
				
			}else{
				//查询现有的明细配置
				List<GrantAuthDetail> grantAuthDetails = grantAuthInfoDao.queryByGrantNo(inGrantAuthDetailVo);
				List<GrantAuthDetail> listGrants = new ArrayList<GrantAuthDetail>();
				//筛选出正在修改的那条数据
				for (GrantAuthDetail grantAuthDetailT : grantAuthDetails) {
					if( !grantAuthDetailT.getDetailId().equals( grantAuthDetail.getDetailId() ) ){
						listGrants.add(grantAuthDetailT);
					}
				}
					
				if(listGrants != null && listGrants.size()>0){
					for(int i=0;i<listGrants.size();i++){
						int oldCrdMax = ( listGrants.get(i).getCrdMaxAmt() ).intValue();
						int oldCrdMix = ( listGrants.get(i).getCrdMinAmt() ).intValue();
							
						if(crdMinAmt <= oldCrdMix && crdMaxAmt > oldCrdMix){
							//最小授信金额和最大金额不能交叉
							return -2;
						}else if(crdMinAmt > oldCrdMix && crdMinAmt < oldCrdMax){
							//最小授信金额与最大金额不能交叉
							return -2;
						}
					}
				}
				//通过校验则修更新
				num = grantAuthInfoDao.updateByDetailId(grantAuthDetail);
					
			}
			
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inGrantAuthDetailVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public GrantAuthDetailVO queryByDetailId(GrantAuthDetailVO inGrantAuthDetailVo) {
		
		logger.debug("当前查询参数信息为:"+ inGrantAuthDetailVo);
		try {
			GrantAuthDetail queryGrantAuthDetail = new GrantAuthDetail();
			beanCopy(inGrantAuthDetailVo, queryGrantAuthDetail);
			GrantAuthDetail queryRslGrantAuthDetail = grantAuthInfoDao.queryByDetailId(queryGrantAuthDetail);
			if (Objects.nonNull(queryRslGrantAuthDetail)) {
				GrantAuthDetailVO outGrantAuthDetailVo = beanCopy(queryRslGrantAuthDetail, new GrantAuthDetailVO());
				logger.debug("当前查询结果为:"+ outGrantAuthDetailVo.toString());
				return outGrantAuthDetailVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
	 
	 public List<GrantAuthDetailVO> queryAllGrantDetail(GrantAuthDetailVO inGrantAuthDetailVo) {
			
		logger.debug("当前查询参数信息为:"+ inGrantAuthDetailVo);
		try {
			GrantAuthDetail queryGrantAuthDetail = new GrantAuthDetail();
			beanCopy(inGrantAuthDetailVo, queryGrantAuthDetail);
			List<GrantAuthDetail> queryRslGrantAuthDetails = grantAuthInfoDao.queryAllGrantDetail(inGrantAuthDetailVo);
			if (Objects.nonNull(queryRslGrantAuthDetails)) {
				List<GrantAuthDetailVO> outGrantAuthDetailVo = beanCopy(queryRslGrantAuthDetails, new GrantAuthDetailVO());
				logger.debug("当前查询结果为:"+ outGrantAuthDetailVo.toString());
				return outGrantAuthDetailVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
	/**
	 * 根据授权对象查询
	 */
	public int queryByGrantVal(GrantAuthDetailVO inGrantAuthDetailVo) {
		
		logger.debug("当前查询参数信息为:"+ inGrantAuthDetailVo);
		int num = 0;
		try {
			GrantAuthDetail queryGrantAuthDetail = new GrantAuthDetail();
			beanCopy(inGrantAuthDetailVo, queryGrantAuthDetail);
			num  = grantAuthInfoDao.queryByGrantVal(queryGrantAuthDetail);
			
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return num;
	}
	 
	 @SuppressWarnings("unchecked")
	public List<GrantAuthDetailVO> queryByGrantValAndGrantNo(GrantAuthDetailVO inGrantAuthDetailVo) {

			logger.debug("根据授权编号查询本人所属数据信息的参数信息为:");
			List<GrantAuthDetailVO> list = null;
			try {
				List<GrantAuthDetail> grantAuthDetails = grantAuthInfoDao.queryByGrantValAndGrantNo(inGrantAuthDetailVo);
				logger.debug("根据授权编号查询本人所属数据信息的结果集数量为:"+ grantAuthDetails.size());
				pageSet(grantAuthDetails, inGrantAuthDetailVo);
				list = (List<GrantAuthDetailVO>) beansCopy(grantAuthDetails, GrantAuthDetailVO.class);
			} catch (Exception e) {
				logger.error("数据转换出现异常!", e);
			}
			
			return list;
		
		}
	
	/**
	 * 根据授权编号查询
	 */
	
	@SuppressWarnings("unchecked")
	@Override
	public List<GrantAuthDetailVO> queryByGrantNo(GrantAuthDetailVO grantAuthDetailVo) {

		logger.debug("根据授权编号查询本人所属数据信息的参数信息为:");
		List<GrantAuthDetailVO> list = null;
		try {
			List<GrantAuthDetail> grantAuthDetails = grantAuthInfoDao.queryByGrantNo(grantAuthDetailVo);
			logger.debug("根据授权编号查询本人所属数据信息的结果集数量为:"+ grantAuthDetails.size());
			pageSet(grantAuthDetails, grantAuthDetailVo);
			list = (List<GrantAuthDetailVO>) beansCopy(grantAuthDetails, GrantAuthDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<GrantAuthDetailVO> queryAllDetailOwner(GrantAuthDetailVO grantAuthDetailVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<GrantAuthDetailVO> list = null;
		try {
			List<GrantAuthDetail> grantAuthDetails = grantAuthInfoDao.queryAllDetailOwnerByPage(grantAuthDetailVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ grantAuthDetails.size());
			pageSet(grantAuthDetails, grantAuthDetailVo);
			list = (List<GrantAuthDetailVO>) beansCopy(grantAuthDetails, GrantAuthDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<GrantAuthDetailVO> queryAllDetailCurrOrg(GrantAuthDetailVO grantAuthDetailVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<GrantAuthDetail> grantAuthDetails = grantAuthInfoDao.queryAllDetailCurrOrgByPage(grantAuthDetailVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+grantAuthDetails.size());
		List<GrantAuthDetailVO> list = null;
		try {
			pageSet(grantAuthDetails, grantAuthDetailVo);
			list = (List<GrantAuthDetailVO>) beansCopy(grantAuthDetails, GrantAuthDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<GrantAuthDetailVO> queryAllDetailCurrDownOrg(GrantAuthDetailVO grantAuthDetailVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<GrantAuthDetail> grantAuthDetails = grantAuthInfoDao.queryAllDetailCurrDownOrgByPage(grantAuthDetailVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ grantAuthDetails.size());
		List<GrantAuthDetailVO> list = null;
		try {
			pageSet(grantAuthDetails, grantAuthDetailVo);
			list = (List<GrantAuthDetailVO>) beansCopy(grantAuthDetails, GrantAuthDetailVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	
}
